﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Linq;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Plumbing;
using YArchitech.LIB;
using YArchitech.MEP;

namespace YArchitech.Plumbing
{
	public class HYDrainageSystemSetting
	{
		public HYMaterial Material { get; set; }

		public List<HYMaterial> ListMaterial
		{
			get
			{
				return this.listMaterial;
			}
		}

		public string ElbowFamilyFile { get; set; }

		public string TeeFamilyFile { get; set; }

		public string CrossFamilyFile { get; set; }

		public string SBendFamilyFile { get; set; }

		public string PBendFamilyFile { get; set; }

		public string CleanMouthFamilyFile { get; set; }

		public string VentilationCapFamilyFile { get; set; }

		public string FDrainFamilyFile { get; set; }

		public string CheckMouthFile { get; set; }

		public string PipingSystemName { get; set; }

		public PipingSystemType PipingSystemType { get; set; }

		public string PipeTypeName { get; set; }

		public PipeType HYPipeType { get; set; }

		public double RPLength { get; set; }

		private HYDrainageSystemSetting()
		{
			this.Load();
		}

		public static HYDrainageSystemSetting Instance
		{
			get
			{
				return new HYDrainageSystemSetting();
			}
		}

		public List<HYMaterial> GetAllMaterials()
		{
			return HYMaterial.ReadAllMaterials(Path.Combine(Product.DataLocation, "HYPipeStandard_PS.xml"));
		}

		public bool Load()
		{
			this.listMaterial = this.GetAllMaterials();
			this.GetConfigFile();
			return true;
		}

		public void InitialPipeSystemType(Autodesk.Revit.DB.Document doc)
		{
			OperateSegmentFitting operateSegmentFitting = new OperateSegmentFitting(doc);
			this.PipingSystemType = operateSegmentFitting.GetPipingSystemType();
			this.HYPipeType = operateSegmentFitting.GetPipeTypeByTypeName(this.PipeTypeName);
			foreach (HYMaterial hymaterial in this.ListMaterial)
			{
				if (!(hymaterial.Name != this.PipeTypeName))
				{
					this.Material = hymaterial;
					break;
				}
			}
		}

		public FamilySymbol GetSettedFamilySymbol(Autodesk.Revit.DB.Document doc, HYFamilySymbolSign fSymbolSign)
		{
			FamilySymbol result = null;
			OperateSegmentFitting operateSegmentFitting = new OperateSegmentFitting(doc);
			switch (fSymbolSign)
			{
			case HYFamilySymbolSign.Elbow:
				result = operateSegmentFitting.LoadFamilySymbolByName(this.ElbowFamilyFile, fSymbolSign);
				break;
			case HYFamilySymbolSign.Tee:
				result = operateSegmentFitting.LoadFamilySymbolByName(this.TeeFamilyFile, fSymbolSign);
				break;
			case HYFamilySymbolSign.Cross:
				result = operateSegmentFitting.LoadFamilySymbolByName(this.CrossFamilyFile, fSymbolSign);
				break;
			case HYFamilySymbolSign.SBend:
				result = operateSegmentFitting.LoadFamilySymbolByName(this.SBendFamilyFile, fSymbolSign);
				break;
			case HYFamilySymbolSign.PBend:
				result = operateSegmentFitting.LoadFamilySymbolByName(this.PBendFamilyFile, fSymbolSign);
				break;
			case HYFamilySymbolSign.CleanMouth:
				result = operateSegmentFitting.LoadFamilySymbolByName(this.CleanMouthFamilyFile, fSymbolSign);
				break;
			case HYFamilySymbolSign.VentilationCap:
				result = operateSegmentFitting.LoadFamilySymbolByName(this.VentilationCapFamilyFile, fSymbolSign);
				break;
			case HYFamilySymbolSign.FloorDrain:
				result = operateSegmentFitting.LoadFamilySymbolByName(this.FDrainFamilyFile, fSymbolSign);
				break;
			case HYFamilySymbolSign.CheckMouth:
				result = operateSegmentFitting.LoadFamilySymbolByName(this.CheckMouthFile, fSymbolSign);
				break;
			}
			return result;
		}

		public void GetConfigFile()
		{
			XElement xelement = XElement.Load(Product.UserConfigFile).Element("Plumbing");
			this.RPLength = Convert.ToDouble(xelement.Attribute("RiserPipeLength").Value);
			this.ElbowFamilyFile = xelement.Element("ElbowFitting").Value;
			this.TeeFamilyFile = xelement.Element("TeeFitting").Value;
			this.CrossFamilyFile = xelement.Element("CrossFitting").Value;
			this.SBendFamilyFile = xelement.Element("STraps").Value;
			this.PBendFamilyFile = xelement.Element("PTraps").Value;
			this.CleanMouthFamilyFile = xelement.Element("CleanMouth").Value;
			this.VentilationCapFamilyFile = xelement.Element("InspectionPort").Value;
			this.FDrainFamilyFile = xelement.Element("FloorDrain").Value;
			this.PipeTypeName = xelement.Element("PipeTypeName").Value;
			this.CheckMouthFile = xelement.Element("CheckMouth").Value;
		}

		public void Save()
		{
			XElement xelement = XElement.Load(Product.UserConfigFile);
			XElement xelement2 = xelement.Element("Plumbing");
			xelement2.SetElementValue("ElbowFitting", this.ElbowFamilyFile);
			xelement2.SetElementValue("TeeFitting", this.TeeFamilyFile);
			xelement2.SetElementValue("CrossFitting", this.CrossFamilyFile);
			xelement2.SetElementValue("STraps", this.SBendFamilyFile);
			xelement2.SetElementValue("PTraps", this.PBendFamilyFile);
			xelement2.SetElementValue("CleanMouth", this.CleanMouthFamilyFile);
			xelement2.SetElementValue("InspectionPort", this.VentilationCapFamilyFile);
			xelement2.SetElementValue("FloorDrain", this.FDrainFamilyFile);
			xelement2.SetElementValue("PipeTypeName", this.PipeTypeName);
			xelement2.SetElementValue("CheckMouth", this.CheckMouthFile);
			xelement.Save(Product.UserConfigFile);
		}

		public void Save(string fileName)
		{
		}

		private List<HYMaterial> listMaterial;

		private static HYDrainageSystemSetting m_Instance;
	}
}
